package cn.tedu.controller;

import cn.tedu.controller.BaseAndEx.BaseController;
import cn.tedu.entity.Cart;
import cn.tedu.service.port.CartService;
import cn.tedu.util.JsonResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/cart")
public class CartController extends BaseController {

    @Resource
    private CartService cartService ;

    /**
     * 商品添加购物车功能
     * @param cart      商品信息
     * @param session   用于获取uid
     */
    @ResponseBody
    @RequestMapping(value = "/add.do", method = RequestMethod.POST)
    public JsonResult handleAddToCart(Cart cart, HttpSession session){
        System.out.println("cart:" +cart);
        Integer uid = getUidSession(session);
        cart.setUid(uid);
        cartService.add(cart);
        return new JsonResult();
    }
//--------------------------------------------------------------------------

    /**
     * 携商品数据跳转值立即付款
     * @param session   用于获取uid
     * @param modelMap  转发数据
     */
    @RequestMapping("/list.do")
    public String showList(
            HttpSession session,
            ModelMap modelMap) {
        // 获取uid
        Integer uid = getUidSession(session);
        // 获取数据
        List<Cart> carts
                = cartService.getCartList(uid);
        // 封装数据，以准备转发
        modelMap.addAttribute("carts", carts);
        // 转发
        return "cart_list";
    }

}
